<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8">
        <title>修改密码</title>
        <link th:href="@{/layui/css/layui.css}" rel="stylesheet"/>
    </head>
    <body>
        <form class="layui-form" action="" lay-filter="user_password">
            <div class="layui-form-item">
                <label class="layui-form-label">原密码</label>
                <div class="layui-input-block" style="width: 50%;">
                    <input type="password" name="raw_password" lay-verify="raw_password_verify" autocomplete="off" placeholder="请输入原密码" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">新密码</label>
                <div class="layui-input-block" style="width: 50%;">
                    <input type="password" name="new_password" lay-verify="new_password_verify" autocomplete="off" placeholder="请输入新密码" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="update_password">提交</button>
                </div>
            </div>
        </form>
        <script th:src="@{/layui/layui.js}"></script>
        <script th:inline="javascript">
            //Demo
            layui.use('form', function(){
                var form = layui.form;
                var $ = layui.jquery;

                form.verify({
                    raw_password_verify: function (value) {
                        if(value.length <= 0){
                            return '请输入原密码';
                        }
                    },
                    new_password_verify: function (value) {
                        if(value.length <= 0){
                            return '请输入新密码';
                        }
                    }
                });



                form.on('submit(update_password)', function(data){
                    var index = layer.load();

                    var request_obj = { "rawPassword": data.field.raw_password,
                        "newPassword": data.field.new_password};
                    var token = layui.data("user_token");

                    console.log(request_obj);
                    console.log(token);
                    $.ajax({
                        type: 'post',
                        url: '/sys-user/modify-password',
                        dataType: 'json',
                        contentType: 'application/json; charset=utf-8',
                        data: JSON.stringify(request_obj),
                        headers: {
                            token:token.token
                        },
                        success: function (raw_data) {
                            if (raw_data.code === 1){
                                parent.layer.msg("修改密码成功!", {time: 1000}, function () {
                                    //重新加载父页面
                                    parent.location.reload();
                                });
                            } else {
                                layer.msg(raw_data.message);
                            }
                            layer.close(index);
                        }
                    });
                    return false;
                });
            });
        </script>
    </body>
</html>